package com.ruoyi.manager.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.manager.domain.Member;
import com.ruoyi.manager.service.IMemberService;
import com.ruoyi.manager.utils.BaseTextUtil;
import org.apache.ibatis.transaction.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/manager/user")
public class UserController {

    @Autowired
    private IMemberService memberService;

    @GetMapping
    public Member getUser() {
        Long userId = BaseTextUtil.getCurrentId();
        if (userId != null) {
            return memberService.selectMemberById(userId);
        }
        return null;
    }

    @GetMapping("currentUseReservations")
    public AjaxResult getCurrentUseReservations(Integer pageNum, Integer pageSize,
                                                Integer userId, Integer type,
                                                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate createTime,
                                                Integer status) {

        //参数校验 TODO
        if(userId == null){
            return AjaxResult.error("用户不存在，请重新登录");
        }


        PageHelper.startPage(pageNum, pageSize);
        Page<Transaction> page = memberService.getReservations(userId, type, createTime, status);


        Map<String ,  Object> map = new HashMap<>();
        map.put("record" , page.getResult());
        map.put("total" , page.getTotal());
        return AjaxResult.success(map);
    }


    /**
     * 用户取消预约
     * @param id
     * @return
     */
    @GetMapping("cancelReservation")
    public AjaxResult cancelReservation(@RequestParam("id") Long id){
        memberService.cancelReservation(id);
        return AjaxResult.success();
    }


    /**
     * 充值
     * @return
     */
    @GetMapping("recharge")
    public AjaxResult recharge(@RequestParam("phone") String phone ,@RequestParam("password") String password ,@RequestParam("amount") Long amount ){
        if (amount == null || amount <= 0){
            return AjaxResult.error("充值金额不能小于0");
        }

        memberService.recharge(phone, password, amount);

        return AjaxResult.success();
    }




    /**
     * 预约审核
     * @param id
     * @return
     */
    @GetMapping("verifyReservation")
    public AjaxResult verifyReservation(@RequestParam("id") Long id){
        memberService.verifyReservation(id);
        return AjaxResult.success();
    }


    /**
     * 退出登录
     */
    @GetMapping("logout")
    public AjaxResult logout(@RequestParam("id") Long id){
        // 参数验证
        if (id == null) {
            return AjaxResult.error("用户ID不能为空");
        }
        return AjaxResult.success();
    }


}
